#include "HttpServer.hpp"
#include "Daemon.hpp"
#include "Log.hpp"
using namespace LogMudule;
void Login(HttpRequest& req, HttpResponse* resp)
{
    // 根据req，动态构建resp
    // req.Path() /login
    // req.Args() username=xxx&password=xxx
    LOG(LogLevel::DEBUG) << "---------登录模块---------" << req.Path() << ", " << req.Args();
    // 1. 解析req.Args()

    // 2. 访问数据库

    // 3. 登陆成功
    std::string body = req.GetContent("wwwroot/success.html");
    if (body.empty()) {
        LOG(LogLevel::ERROR) << "Failed to load success.html";
    }
    resp->SetCode(200);
    resp->SetHeader("Content-Length: ",std::to_string(body.size()));
    resp->SetHeader("Content-Type", "text/html");
    resp->SetHeader("Set-Cookie", "username=zxw;password=123456");
    resp->SetBody(body);

    // resp->SetCode(302);
    // resp->SetHeader("Location", "/");
}

void Search(HttpRequest& req, HttpResponse* resp)
{

}

void Test(HttpRequest& req, HttpResponse* resp)
{
    LOG(LogLevel::DEBUG) << req.Path() << ", " << req.Args();
}

int main(int argc, char* argv[])
{
    if(argc!= 2)
    {
        std::cout << "Usage: " << argv[0] << " port" << std::endl;
        return 1;
    }
    // Damon(false,false);  
    auto httpserver = std::make_shared<HttpServer>(std::stoi(argv[1]));
    // 要让服务器具有登录功能
    httpserver->Resgiter("/login",Login);
    httpserver->Resgiter("/test",Test);
    httpserver->start();
    return 0;
}